import GameConfig from '../config/GameConfig.js';

/**
 * 食物类
 */
export default class Food {
    /**
     * 初始化食物
     */
    constructor(game) {
        this.game = game;
        this.position = this.generatePosition();
    }

   /**
     * 生成食物位置
     * @returns {Object} 食物的新位置
     */
   generatePosition() {
    const dimensions = this.game.getGameAreaDimensions();
    let position;
    
    do {
        position = {
            x: Math.floor(Math.random() * dimensions.width),
            y: Math.floor(Math.random() * dimensions.height)
        };
    } while (
        this.game.snake && 
        this.game.snake.segments.some(segment => 
            segment.x === position.x && segment.y === position.y
        )
    );
    
    return position;
  }

    /**
     * 重新生成食物
     */
    regenerate() {
        this.position = this.generatePosition();
    }

    /**
     * 检查是否与蛇身重叠
     * @param {Snake} snake - 蛇对象
     * @returns {boolean} 是否重叠
     */
    checkOverlapWithSnake(snake) {
        return snake.segments.some(segment => 
            segment.x === this.position.x && segment.y === this.position.y
        );
    }

    /**
     * 获取食物位置
     * @returns {Object} 食物坐标
     */
    getPosition() {
        return this.position;
    }
}